<?php
/**
 +--------------------------------
 * RefundAction.class.php （用户退款）
 * date:2013.12.16
 * @author tanbin
 +--------------------------------
 *
 */
class RechargeAction extends BaseAction {
	protected $money;
	function _initialize() {
		parent::_initialize ();		
		$this->dao = M('UserRefund');
		import ( 'Com.Money' );
		import ( 'Com.User' );
		$this->money = new Money ();
		
		$this->vcw= array(
					 'guo999','edwin','adminwei','guojiacheng','caiwusmw','guan100221','zhangmei','szmei','zhangruizhuan'
		);
		// add by tb 暂时
		if (!in_array( $this->username, $this->vcw, true ) ) {
			$this->_ajm ( 300, '权限不足!', 0 );
			exit;
		}
	}
	
	/*
	 * 退款记录列表
	 * */
	function index(){
		
		$where = array();
		if($_REQUEST['moneytype']){
			$where['moneytype'] = $_REQUEST['moneytype'];
		}
		if( $_REQUEST['opttype'] ) 
			$where['refundtype'] = $_REQUEST['opttype'] ? $_REQUEST['opttype'] : 'userrecharge';
		else $where['refundtype'] = array('in',array('deduct','userrecharge'));
		$where = parent::create_where($where);
		$count = $this->dao->where($where)->count();
		if($count){
			$page=new Page($count,$this->listrows);
			$list = $this->dao->where($where)->order($this->order)->limit($page->firstRow.','.$page->listRows)->select();
			$this->assign('list',$list);
		}
		$this->assign("totalCount",$count);
		$this->assign('where',$where);
		$this->display();
	}	
	
	
	
	/*
	 * 退款申请操作
	* */
	function edit(){
		//获取退款类型
		$refundtype=M("SystemConfig")->field('value,title')->where('element="recharge" or element="deduct"')->select();
		
		if($_REQUEST['id']){
			$info = $this->dao->find($_REQUEST['id']);
			$this->assign('info',$info);
		}
		$this->assign('refundtype',$refundtype);
		$this->display();
	}
	
	/*
	 * 保存退款申请信息
	* */
	function save(){
		$data = $_REQUEST['data'];
		$data['userid'] = $_REQUEST['user_id'];
		$data['username'] = $_REQUEST['user_username'];
		$data['aid'] = $this->userid;
		$data['money'] = abs($data['money'])*100;			
		$data['ctime'] = time();
		$data['authstatus'] = 2;
		$data['status'] = 1;
		$content[] = array(
				'authuid'=>$this->userid,
				'authstatus'=>2,
				'time'=>time(),
				'msg'=>$data['refundtype']=='userrecharge' ? '用户充值' : '用户扣款');
		$data['content']=serialize($content);
			
		
		//退款操作
	if ($data['userid'] && $data['money'] && $data['refundtype']=='userrecharge' && $data['moneytype']) {
			
				//获取退款类型
			$urs = $this->money->recharge ( $data['userid'], $data['moneytype'], $data['money']/100, array (
					'action_userid' => $this->userid,
					'cip' => get_client_ip (),
					'bzvu' => '用户充值',
					'changtype' => 'ADMIN_ADD',
			) );
			$reid = $this->dao->add($data);
			if($reid) $this->_ajm(200,'充值成功！',0,U ( 'Recharge/index' ));
			else $this->_ajm(300,'充值失败！');
				
		}elseif ($data['userid'] && $data['money'] && $data['refundtype']=='deduct' && $data['moneytype']) {
			//验证余额
			$m = M("UserMoney");
			$where['userid'] = $data['userid'];
			$where['subject'] = $data['moneytype'];
			$money = $m->field('value')->where($where)->find();
			if($money['value']<$data['money']){
				$this->_ajm(300,'用户'.$data['moneytype'].'不够，扣款失败！',0,U ( 'Recharge/index' ));
				exit;
			}
			//获取退款类型
			$urs = $this->money->recharge ( $data['userid'], $data['moneytype'], $data['money']/100, array (
					'action_userid' => $this->userid,
					'cip' => get_client_ip (),
					'bzvu' => '用户扣款',
					'changtype' => 'ADMIN_ADD',
					'oper' => '-',
			) );
			
			$reid = $this->dao->add($data);
			$this->_ajm(200,'扣款成功！',0,U ( 'Recharge/index' ));
			
				
		}else{
			$this->_ajm(300,'用户或者金额或者充值类型或者金额类型操作 有误！');
		}
				
	}
	
	/**
	 * 线下转帐充值
	 * */
	function bank(){
		$dao = M('UserBankPay');
		$user_id = $_POST['user_id']; 
		$user_name = $_POST['user_username'];
		$user = new User();
		$info=$user->get_data("User", array('id' => $user_id), "mobile,email", false);
		
		if(!$user_id || !$user_name || !$info){
			$this->_ajm(300,'用户不存在！试试从弹出的框中选择用户！');
			exit;
		}
		
		$data['moneyprice']=intval($_POST['price']*100);
		if(empty($data['moneyprice']) || intval($data['moneyprice'])<0){
			$this->_ajm (300,"充值金额填写错误。");
			exit;
		}
		
		$data['paytype']= 2; //线下充值
		//处理流水号		
		if(empty($_POST['liushuisn'])){
			$liushuisn = '线下充值';
		}else{
			$liushuisn = $_POST['liushuisn'];
			//判断流水唯一
			$where['liushuisn'] = $liushuisn;
			$onlysn = $dao->where($where)->find();
			if($onlysn){
				$this->_ajm (300,"流水号重复");
				exit;
			}
		}
		//设置转账描述信息
		$str='('.$user_id.')'.$user_name.'->('.$this->userid.')'.$this->username.'于【'.date('Y-m-d ：H:m').'】';
		$data['content']=$str;	

		//默认 核查属实，等待操作入账
		$re_content [] = array (
				'aid' => $this->userid,
				'time' => time (),
				'msg' => '核查属实，等待操作入账。' . '<br>' 
		);
		$data['reply_content'] = serialize ( $re_content );
		
		$data['ip']=get_client_ip(1);
		$data['createtime']=time();
		$data['status']=1;
		$data['typeid']=1;
		$data['userid']=$user_id;
		$data['username']=$user_name;
		$data['title']=$user_name.' '.date('Y-m-d').'日'.date("H").'时'.'流水号:'.$liushuisn;
		$data['liushuisn']=$liushuisn;
		$data['telephone']=$info['mobile'];
		$data['email']=$info['email'];
		$data['only']=short(md5($liushuisn.'_'.$data['userid'].'_'.$data['moneyprice']));
		
		$res = $dao->add($data);
		if($res){
			$this->_ajm(200,'添加成功！','Money/payinfo');		
		}else{
			$this->_ajm(300,'提交失败，请重试!');
		}
	}
	
	/**
	 * 删除线下转帐充值记录
	 * */
	function del_userbank(){
		$dao = M('UserBankPay');
		$id = $_GET['id'];
		if(!$id){
			$this->_ajm(300,'数据不存在，请重试！');
			exit;
		}
		
		$reid = $dao->delete($id);
		if($reid){
			$this->_ajm(200,'删除成功！',1);
		}else{
			$this->_ajm(300,'删除失败！',1);
		}
		
	}
	
	
}
	
